package com.woniuxy.properties;

import com.rabbitmq.client.*;
import com.woniuxy.utils.RabbitConnection;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: linKe
 * @Date: 2022/1/10 16:37
 * @FileName: Consumer
 * @version: 1.0
 * @Description: 消费者
 */
@Log4j2
public class ConsumerEmailProperties {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 连接通道
        Channel channel = RabbitConnection.getConnection().createChannel();


        channel.queueDeclare("queueEmailTopic",true,false,false,null);

        channel.exchangeDeclare("exchangeTopic",BuiltinExchangeType.TOPIC);

        channel.queueBind("queueEmailTopic","exchangeTopic","#.email.#");

        channel.basicConsume("queueEmailTopic",true,new DefaultConsumer(channel){
            /**
             * 处理消息的业务代码
             * @param consumerTag 消息标记，系统会自动分配
             * @param envelope  信封，封装和当前消息相关的消息，后面的手动确认要是用
             * @param properties    发送消息时指定的附加属性
             * @param body          消息体
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body:"+new String(body));

            }
        });
    }
}
